﻿using blackhouse.NemoBlog.Business;
using blackhouse.WebNemo.Business.Groups;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace blackhouse.NemoBlog.Web {
    public class PostGroupExtension : GroupExtension {

        #region Fields

        private NemoBlogSettings blogSettings;
        private int[] editorHideColumns;

        #endregion

        #region Constructors

        public PostGroupExtension(IGroup group)
            : base(group) {
            HttpContext context = HttpContext.Current;
            if (context != null) {
                HttpRequest request = context.Request;
                if (request != null) {
                    int mid = 0;
                    if (Int32.TryParse(request["mid"], out mid)) {
                        this.blogSettings = new NemoBlogSettings(mid);
                        this.editorHideColumns = blogSettings.Configuration.GetEditorHideColumns();
                    }
                }
            }
        }

        #endregion

        #region Methods

        public override bool ShouldUseThisColumnInEditor(GroupColumn column) {
            if (column.ColumnName == "tags" && !this.blogSettings.UseTagSystem) return false;
            if (column.ColumnName == "url" && !this.blogSettings.AllowUserChangeUrl) return false;
            if (column.ColumnName == "shortDescription" && !this.blogSettings.EnableShortDescription) return false;
            if (column.HideForEdition) return false;
            return this.editorHideColumns == null || this.editorHideColumns.Length == 0 || !this.editorHideColumns.Contains(column.ColumnId);
        }

        #endregion

    }
}